"""
Case Type   : 资源池化系统视图
Case Name   : 默认配置下，执行switchover操作，查看系统视图gs_shared_memory_detail
Create At   : 2025/2
Owner       : haomeng
Description :
    1、主节点查看系统表gs_shared_memory_detail & pg_shared_memory_detail()中DMSDrcContext字段是否存在
    2、备节点查看系统表gs_shared_memory_detail & pg_shared_memory_detail()中DMSDrcContext字段是否存在
    3、执行switchover操作切换主备节点
    4、主节点查看系统表gs_shared_memory_detail & pg_shared_memory_detail()中DMSDrcContext字段是否存在
    5、备节点查看系统表gs_shared_memory_detail & pg_shared_memory_detail()中DMSDrcContext字段是否存在
    6、恢复初始环境
Expect      :
    1、存在DMSDrcContext相关字段及信息
    2、存在DMSDrcContext相关字段及信息
    3、切换成功
    4、存在DMSDrcContext相关字段及信息
    5、存在DMSDrcContext相关字段及信息
    6、环境恢复成功
History     :
"""

import os
import unittest

from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from yat.test import Node
from yat.test import macro


class ShareStorageSystemView(unittest.TestCase):

    def setUp(self):
        self.log = Logger()
        self.log.info(f'----{os.path.basename(__file__)} start----')
        self.pri_sh = CommonSH('PrimaryDbUser')
        self.sta_sh = CommonSH('Standby1DbUser')
        self.constant = Constant()

        self.sys_view = ['gs_shared_memory_detail', 'pg_shared_memory_detail()']

    def test_ss_system_view(self):
        text = '-----step1:主节点查看系统表gs_shared_memory_detail & pg_shared_memory_detail()' \
               '中DMSDrcContext字段是否存在  expect:存在DMSDrcContext相关字段及信息-----'
        self.log.info(text)

        for view in self.sys_view:
            sql1 = f"select * from {view} where contextname='DMSDrcContext';"
            self.log.info(sql1)
            res1 = self.pri_sh.execut_db_sql(sql1)
            self.log.info(res1)
            self.assertIn('(1 row)', res1, '存在异常信息')

        text = '-----step2:备节点查看系统表gs_shared_memory_detail & pg_shared_memory_detail()' \
               '中DMSDrcContext字段是否存在  expect:存在DMSDrcContext相关字段及信息-----'
        self.log.info(text)

        for view in self.sys_view:
            sql2 = f"select * from {view} where contextname='DMSDrcContext';"
            self.log.info(sql2)
            res2 = self.sta_sh.execut_db_sql(sql2)
            self.log.info(res2)
            self.assertIn('(1 row)', res2, '存在异常信息')

        text = '-----step3:执行switchover操作切换主备节点  expect:切换成功-----'
        self.log.info(text)

        switchover = self.pri_sh.exec_cm_ctl('switchover', '-n 2',
                                             f'-D {macro.DB_INSTANCE_PATH}')
        self.log.info(switchover)
        self.assertIn(self.constant.SWITCHOVER_SUCCESS_MSG, switchover, '执行失败')

        text = '-----step4:新主节点查看系统表gs_shared_memory_detail & pg_shared_memory_detail()' \
               '中DMSDrcContext字段是否存在  expect:存在DMSDrcContext相关字段及信息-----'
        self.log.info(text)

        for view in self.sys_view:
            sql3 = f"select * from {view} where contextname='DMSDrcContext';"
            self.log.info(sql3)
            res3 = self.sta_sh.execut_db_sql(sql3)
            self.log.info(res3)
            self.assertIn('(1 row)', res3, '存在异常信息')

        text = '-----step5:新备节点查看系统表gs_shared_memory_detail & pg_shared_memory_detail()' \
               '中DMSDrcContext字段是否存在  expect:存在DMSDrcContext相关字段及信息-----'
        self.log.info(text)

        for view in self.sys_view:
            sql4 = f"select * from {view} where contextname='DMSDrcContext';"
            self.log.info(sql4)
            res4 = self.pri_sh.execut_db_sql(sql4)
            self.log.info(res4)
            self.assertIn('(1 row)', res4, '存在异常信息')

    def tearDown(self):
        text = '-----step6:恢复初始环境  expect:恢复成功-----'
        self.log.info(text)

        restore = self.pri_sh.exec_cm_ctl('switchover', '-a')
        self.log.info(restore)
        self.assertIn(self.constant.SWITCHOVER_SUCCESS_MSG, restore, '执行失败')

        status = self.pri_sh.exec_cm_ctl('query', '-Cv')
        self.log.info(status)
        self.assertIn('Primary Normal', status.splitlines()[-1].split('|')[0],
                      '恢复集群初始状态失败')
        self.log.info(f'----{os.path.basename(__file__)} end----')
